package cn.tedu.micro.demo.es;

import cn.tedu.micro.demo.es.po.StudentEsPO;
import cn.tedu.micro.demo.es.repository.StudentEsRepository;
import cn.tedu.micro.demo.es.utils.NickNameGenerator;
import cn.tedu.micro.demo.es.utils.ParseDateUtil;
import cn.tedu.micro.demo.es.utils.StudentDataGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@SpringBootTest
public class EsTest01 {
    @Autowired
    private StudentEsRepository studentEsRepository;

    //1.新增
    //1.1 新增单个文档
    @Test
    public void saveOne() {
        //1.封装一个studentEsPO
        String name = NickNameGenerator.generate();
        Date birth = ParseDateUtil.parse("2010-01-01 00:00:00");
        StudentEsPO studentEsPO = new StudentEsPO(
                UUID.randomUUID().toString().replace("-", ""),
                name,
                18,
                "王翠花喜欢美术,同时唱歌也不错",
                birth
        );
        //2.写入到索引作为文档存储
        studentEsRepository.save(studentEsPO);
    }

    //1.2批量新增
    @Test
    public void saveAll() {
        //1.封装列表studentEsPO
        StudentEsPO studentEsPO1 = new StudentEsPO(
                UUID.randomUUID().toString(),
                NickNameGenerator.generate(),
                19,
                "喜欢音乐,同时体育也不错",
                ParseDateUtil.parse("2011-01-01 00:00:00")
        );
        StudentEsPO studentEsPO2 = new StudentEsPO(
                UUID.randomUUID().toString(),
                NickNameGenerator.generate(),
                20,
                "喜欢电影,同时阅读也不错",
                ParseDateUtil.parse("2012-01-01 00:00:00")
        );
        //封装到列表
        List<StudentEsPO> pos = List.of(studentEsPO1, studentEsPO2);
        //2.调用api
        studentEsRepository.saveAll(pos);
    }

    //2.查询获取文档对象
    //2.1查询单个,通过文档id获取查询的 id指的是每个文档属性_id
    @Test
    public void findOne() {
        //1参数 就是uuid
        String id = "e955781a90904f178aa2cc8f58ecd922";
        //2调用findById方法
        Optional<StudentEsPO> optional = studentEsRepository.findById(id);
        //判断是否命中
        boolean present = optional.isPresent();
        if (present) {
            //命中数据,可以获取文档
            StudentEsPO studentEsPO = optional.get();
            System.out.println(studentEsPO);
        } else {
            System.out.println("没有数据");
        }
    }

    //2.2查询多个 利用ids列表
    @Test
    public void findAll() {
        //1.封装列表
        List<String> ids = List.of(
                "e955781a90904f178aa2cc8f58ecd922",
                "d5658c8e1eef47be9dfd66f6a7c4611c"
        );
        //2.调用方法
        Iterable<StudentEsPO> pos = studentEsRepository.findAllById(ids);

        //3.遍历打印
        for (StudentEsPO po : pos) {
            System.out.println(po);
        }
    }

    //生成一批准备好测试派生的数据
    @Test
    public void generateData() {
        List<StudentEsPO> studentEsPOS = StudentDataGenerator.generateRandomStudents(1000);
        studentEsRepository.saveAll(studentEsPOS);
    }

    //派生案例1: findByNameLike
    @Test
    public void findByNameLike() {
        //1.调用方法
        List<StudentEsPO> pos = studentEsRepository.findByNameLike("万");
        //2.遍历打印
        for (StudentEsPO po : pos) {
            System.out.println(po);
        }
    }

    //派生案例2: findByNameLikeOrAgeLessThan
    @Test
    public void findByNameLikeOrAgeLessThan() {
        //1.调用方法
        List<StudentEsPO> pos =
                studentEsRepository.findByNameLikeOrAgeLessThan("万", 20);
        //2.遍历打印
        for (StudentEsPO po : pos) {
            System.out.println(po);
        }
    }

    //派生案例3:findByAgeLessThandOrderByBirthdayDesc
    @Test
    public void findByAgeLessThanOrderByBirthdayDesc() {
        //1.调用方法
        List<StudentEsPO> pos =
                studentEsRepository.findByAgeLessThanOrderByBirthdayDesc(20);
        //2.遍历打印
        for (StudentEsPO po : pos) {
            System.out.println(po);
        }
    }
    //派生案例4: findByBirthdayBetween
    @Test
    public void findByBirthdayBetween() {
        //1.调用方法
        Date start = ParseDateUtil.parse("2004-01-01 00:00:00");
        Date end = ParseDateUtil.parse("2004-12-01 00:00:00");
        List<StudentEsPO> pos =
                studentEsRepository.findByBirthdayBetween(start, end);
        //2.遍历打印
        for (StudentEsPO po : pos) {
            System.out.println(po);
        }
    }
}